package com.jiaozi.core.table;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.annotation.JSONField;

import com.jiaozi.core.table.annotation.Column;
import com.jiaozi.core.table.annotation.QueryColumn;
import com.jiaozi.core.table.entity.ZjColumn;
import com.jiaozi.core.table.entity.ZjQueryColumn;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class JzTable {
    //展示列
    private List<ZjColumn> zjColumnList;
    private List<ZjQueryColumn> zjQueryColumnList;
    //是否显隐列按钮
    private boolean columnsBotton;
    //刷新按钮
    private boolean refreshBotton;
    //查询条件按钮
    private boolean queryCriteriaBotton;

    //是否分页
    private boolean page ;

    //请求地址
    private String dataUrl ;

    //资源数据
    private Map<String,Object> resData;



    //展示列表
    @JSONField(serialize = false)
    private Class vo;
    //查询参数
    @JSONField(serialize = false)
    private Class param;

    public JzTable(Class vo,Class param){
        this.vo=vo;
        this.param=param;
        zjColumnInfo();
        zjQueryParamInfo();
    }

    public JzTable(Class vo){
        this.vo=vo;
        zjColumnInfo();
    }


    public void zjColumnInfo(){
        if(vo!=null){
            Field[] fields=ReflectUtil.getFields(vo);
            zjColumnList =new ArrayList<>();
            for (Field field : fields) {
                Column column=field.getAnnotation(Column.class);
                if(column!=null){
                    zjColumnList.add(new ZjColumn(field.getName(),column));
                }
            }
            Collections.sort(zjColumnList, Comparator.comparing(ZjColumn::getOrder));
        }
    }


    public void zjQueryParamInfo(){
        if(param!=null){
            Field[] fields=ReflectUtil.getFields(param);
            zjQueryColumnList =new ArrayList<>();
            for (Field field : fields) {
                QueryColumn queryColumn=field.getAnnotation(QueryColumn.class);
                if(queryColumn!=null){
                    zjQueryColumnList.add(new ZjQueryColumn(field.getName(),queryColumn));
                }
            }
            Collections.sort(zjQueryColumnList, Comparator.comparing(ZjQueryColumn::getOrder));
        }
    }



}
